package org.opens.util;

import cn.hutool.core.io.FileUtil;

import java.io.*;
import java.nio.charset.StandardCharsets;

public class FileEncodeUtil {

    /**
     * 判断文件编码是否为utf8
     * @param filePath 文件路径
     */
    public static boolean isUtf8(String filePath) {
        try (
                InputStream inputStream = new FileInputStream(filePath);
                BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
                InputStreamReader inputStreamReader = new InputStreamReader(bufferedInputStream, StandardCharsets.UTF_8);
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        ) {
            String line = null;
            for (int i = 0; i < 4; i++) {
                line = bufferedReader.readLine();
            }

            System.out.println(line);
            byte[] bytes = line.getBytes(StandardCharsets.UTF_8);
            System.out.println(new String(bytes, StandardCharsets.UTF_8));

        } catch (FileNotFoundException e) {
            throw new IllegalArgumentException("文件" + filePath + "不存在!");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        return false;
    }

    public static void main(String[] args) {
        System.out.println(isUtf8("C:\\Users\\Administrator\\Desktop\\temp\\a.txt"));
    }

}
